package com.lenovo.nova.widget.baseview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import com.lenovo.nova.util.MResourceUtil;
import com.lenovo.nova.util.SuccessionEventSimulate;
import com.lenovo.nova.utils.R;

public class CustomDotProgress extends CustomTextView {

	public static final int DEFALUT_PROGRESS = 3;
	public static final int MSG_REFRESH_VOLUME = 0;
	private int DOT_DRAWABLE_RES[] /*= { R.drawable.png_dot_1,
			R.drawable.png_dot_2, R.drawable.png_dot_3 }*/;
	private Bitmap DOT_DRAWABLE[] /*= new Bitmap[DOT_DRAWABLE_RES.length]*/;

	private Paint paint = new Paint();

	private int progress;
	
	private int mMax = DEFALUT_PROGRESS;
	private int mDotProgressLeft;
	
	private SuccessionEventSimulate mSimulate = new SuccessionEventSimulate() {
		int index = 0;
		@Override
		public void looper() {
			index%=mMax;
			setProgress(1 + index++);
		}
	};
	
	public void startAutoProgress(int delay){
		mSimulate.start(100);
		mSimulate.setDelay(delay);
	}
	
	public void setDotProgressLeft(int left){
		mDotProgressLeft = left;
	}
	
	public void endProgress(){
		mSimulate.stop();
	}
	
	public int getProgress() {
		return progress;
	}

	public void setProgress(int progress) {
		if(progress !=   this.progress){
			this.progress = progress;
			invalidate();
		}
	}

	public CustomDotProgress(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initLayout();
	}

	public CustomDotProgress(Context context, AttributeSet attrs) {
		super(context, attrs);
		initLayout();
//		int style = context.getResources().getIdentifier("CustomDotProgressTextView", "styleable",context.getApplicationContext().getPackageName());
		int[] style = MResourceUtil.getIdsByName(context, "styleable", "CustomDotProgressTextView"); 
		TypedArray typeArray = context.obtainStyledAttributes(attrs,
				/*R.styleable.CustomDotProgressTextView*/style);
		
		int N = typeArray.getIndexCount();
		final int value = MResourceUtil.getValue(context, "R.styleable.CustomDotProgressTextView_pogressDotLeft");
		for (int i = 0; i < N; i++) {
			int attr = typeArray.getIndex(i);
			if(attr == value){
				mDotProgressLeft = typeArray.getInteger(attr, 0);
			}
		}
	}

	public CustomDotProgress(Context context) {
		super(context);
		initLayout();
	}

	private void initLayout(){
		DOT_DRAWABLE_RES = new int[] {
				MResourceUtil.getDrawableId(getContext(), "png_dot_1"),
				MResourceUtil.getDrawableId(getContext(), "png_dot_2"),
				MResourceUtil.getDrawableId(getContext(), "png_dot_3"),
//				getContext().getResources().getIdentifier(name, defType, defPackage);
		};
		
		initBitmapArray();
		
		
	}

	private void initBitmapArray() {
		DOT_DRAWABLE = new Bitmap[DOT_DRAWABLE_RES.length] ;
		
		for(int i = 0; i < DOT_DRAWABLE.length; i++){
			DOT_DRAWABLE[i] = BitmapFactory.decodeResource(getResources(), DOT_DRAWABLE_RES[i]);
		}
	}
	
	
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		mSimulate.stop();
	}

	public void setResource(int res[]){
		DOT_DRAWABLE_RES = res;
		initBitmapArray();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		final int dotPadding = 10;
	System.out.println("==========================    onDraw");	
		//calc all dot width
		for (int i = 0; i < mMax; i++) {
			int index = i;
			if(index >= DOT_DRAWABLE.length){
				index = DOT_DRAWABLE.length - 1;
			}
			DOT_DRAWABLE[index].getWidth();
		}
		
		int drawX = mDotProgressLeft;
		if(mDotProgressLeft == 0){
//			drawX = (getWidth() - allDotWidth) / 2;
		}
		
//		if((allDotWidth + mDotProgressLeft) > getWidth()){
//			setWidth(allDotWidth + mDotProgressLeft);
//		}
		
		//draw
		for (int i = 0; i < progress; i++) {
			Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
					DOT_DRAWABLE_RES[DOT_DRAWABLE_RES.length - 1]);
			if (i < DOT_DRAWABLE_RES.length) {
				bitmap = BitmapFactory.decodeResource(getResources(),
						DOT_DRAWABLE_RES[i]);
			}

			canvas.drawBitmap(bitmap, drawX, getHeight() / 2, paint);
			drawX += (bitmap.getWidth() + dotPadding);
		}
		
	}

}
